// pages/shopCart/shopCart.js

import { URL, SER_KEY } from "../../config.js"
import { POST, GET } from "../../utils/request.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    code:0,//用户授权状态
    // totalCount:0,//购物车商品总数
    totalPrice: 0,//购物车商品总价
    cartList: [],//购物车列表
    storeMsg:{},//门店信息
    session_id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    var storeMsg = JSON.parse(wx.getStorageSync('shopData'))
    that.setData({
      storeMsg: storeMsg
    })
   that.getCart()
  },


  // 获取购物车列表
  getCart:function(){
    let that=this
    let shopId = wx.getStorageSync('id')
    let sessionId = wx.getStorageSync('session_id')
    let url = URL.HTTP_BASE_INTRANET + SER_KEY.SER_KEY6
    let data = {
      appId: getApp().globalData.appId,
      shopId: shopId,
      sessionId: sessionId
    }
    GET(url,data,(resData)=>{
      console.log(resData)
      if (resData.data.res == "SUCCESS"){
        wx.stopPullDownRefresh()
        wx.hideNavigationBarLoading()
        let data = resData.data
        console.log(data)
        var num = data.totalCount; //购物车商品数量
        that.setData({
          totalCount: num,
          totalPrice: data.totalPrice,
          cartList: data.lcart,
        })
        if (num === 0 || !num) {
          wx.removeTabBarBadge({ //移除tabbar右上角的文本
            index: 1, //tabbar下标
          })
        } else {
          wx.setTabBarBadge({ //tabbar右上角添加文本
            index: 1, ////tabbar下标
            text: num + '', //显示的内容
          })
        };
      }
      
    })
  },

  HZL_jian:function(e){
    let that=this
    let cartId = e.currentTarget.dataset.id
    let shopId = wx.getStorageSync('id')
    let sessionId = wx.getStorageSync('session_id')
    let url = URL.HTTP_BASE_INTRANET + SER_KEY.SER_KEY7
    let data = {
      appId: getApp().globalData.appId,
      sessionId: sessionId,
      shopId: shopId,
      cartId: cartId
    }
    GET(url, data, (resData) => { 
        if (resData.data.res === 'SUCCESS') {
          that.getCart()
        }
      })

    
  },

  HZL_jia:function(e){
    
    let that = this
    let cartId = e.currentTarget.dataset.id
    let sessionId = wx.getStorageSync('session_id')
    let url = URL.HTTP_BASE_INTRANET + SER_KEY.SER_KEY8
    let data = {
      appId: getApp().globalData.appId,
      sessionId: sessionId,
      cartId: cartId
    }
    GET(url, data, (resData) => { 
        if (resData.data.res === 'SUCCESS'){
          that.getCart()
        }
      
      })

    
  },
  // 清空购物车
  clearCart:function(){
    let that = this
    wx.showModal({
      title: '提示',
      content: '确定要清空购物车？',
      showCancel: true,//是否显示取消按钮
      cancelText: "否",//默认是“取消”
      confirmText: "是",//默认是“确定”
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          //点击确定
          let url = URL.HTTP_BASE_INTRANET + SER_KEY.SER_KEY9
          let shopId = wx.getStorageSync('id')
          let sessionId = wx.getStorageSync('session_id')
          let data = {
            appId: getApp().globalData.appId,
            sessionId: sessionId,
            shopId: shopId
          }

          GET(url, data, (resData) => {
            if (resData.data.res === 'SUCCESS') {
              that.getCart()
            }
          })
        }
      },
      fail: function (res) { },//接口调用失败的回调函数
      complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
    })
   
  },
  toAuthorizy:function(){    
    wx.navigateTo({
      url: '../authorized-login/authorized-login'
    })
  },
  strol:function(){
    wx.switchTab({
      url: '../index/index',
    })
  },
  //点击头像微信授权
  bindGetUserInfo: function (e) {
    console.log(e)
    wx.navigateTo({
      url: '../authorized-login/authorized-login',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that=this
    var sessionId=wx.getStorageSync('session_id')
    console.log(sessionId)
    if (!sessionId){
      that.setData({
        code: 1
      })
    }else{
      that.setData({
        code: 0
      })
    }
    wx.checkSession({
      success(e) {
        //session_key 未过期，并且在本生命周期一直有效
        that.getCart()
      },
      fail() {
        // session_key 已经失效，需要重新执行登录流程
        // that.toAuthorizy()
        console.log('失效')
        that.setData({
          code: 1
        })
      }
    })
  },
  login(){
    wx.navigateTo({
      url: '../authorized-login/authorized-login',
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getCart()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})